package com.changgou.canal.controller;

import com.changgou.canal.util.FastDFSUtil;
import com.changgou.canal.file.FastDFSFile;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 秦川熙
 */
@Controller
@RequestMapping(value = "/upload/file")
public class FileUploadController {

    /**
     * 文件上传
     */
    @PostMapping
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception {
        /**
         * 封装文件信息
         * file.getOriginalFilename() 文件名字 1.jpg
         * file.getBytes() 文件字节数组
         * file.getOriginalFilename 获取文件扩展名
         */
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),
                file.getBytes(),
                file.getOriginalFilename()
        );

        //调用FastDFSUtil工具类将文件传入到FastFDS中
        String[] uploads = FastDFSUtil.upload(fastDFSFile);

        //拼接访问地址  url = "http://192.168.211.132:8080/" + uploads[0] + "/" + uploads[1]
        String url = FastDFSUtil.getTrackerInfo() + "/" + uploads[0] + "/" + uploads[1];
        System.out.println(url);
        return new Result(true, StatusCode.OK, "上传成功", url);
    }


}
